#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->tabWidget->setCurrentIndex(0);
    ui->showTabBtn->setEnabled(false);

    // 初始化按钮
    slotUpdateBtn();

    // 连接tab修改时对按键的影响
    connect(this,&Widget::sigUpdateBtn,this,&Widget::slotUpdateBtn);

    // 显示全部tab
    connect(ui->showTabBtn,&QPushButton::clicked,ui->tabWidget,[&](){
        // findChildren 返回的是 QList<QTabBar*>，而一个 tabWidget 只有一个 QTabBar
       auto tabBar = ui->tabWidget->findChildren<QTabBar*>();
       tabBar.at(0)->show();
       ui->hideTabBtn->setEnabled(true);
       ui->showTabBtn->setEnabled(false);
    });

    // 隐藏全部tab
    connect(ui->hideTabBtn,&QPushButton::clicked,ui->tabWidget,[&](){
        auto tabBar = ui->tabWidget->findChildren<QTabBar*>();
        tabBar.at(0)->hide();
        ui->showTabBtn->setEnabled(true);
        ui->hideTabBtn->setEnabled(false);
    });

    // 添加左边tab
    connect(ui->addLeftBtn,&QPushButton::clicked,ui->tabWidget,[&](){
       // 获取当前的tab的index，获取qtabwidget的tabbar对象
       // 由tabbar进行插入tab，同时修改相关tab的名称
       int idx = ui->tabWidget->currentIndex();
       ui->tabWidget->insertTab(idx,new QWidget(ui->tabWidget),QString("tab_%1").arg(idx));
       for(int i=idx+1;i<ui->tabWidget->count();++i){
           ui->tabWidget->setTabText(i,QString("tab_%1").arg(i));
       }
       ui->tabWidget->setCurrentIndex(idx);
       emit sigUpdateBtn();
    });

    // 添加右边tab
    connect(ui->addRightBtn,&QPushButton::clicked,ui->tabWidget,[&](){
       int idx = ui->tabWidget->currentIndex()+1;
       // auto tabBar = ui->tabWidget->findChildren<QTabBar*>().at(0);
       ui->tabWidget->insertTab(idx,new QWidget(ui->tabWidget),QString("tab_%1").arg(idx));
       for(int i=idx+1;i<ui->tabWidget->count();++i){
           // tabBar->setTabText(i,QString("tab_%1").arg(i));
           ui->tabWidget->setTabText(i,QString("tab_%1").arg(i));
       }
       ui->tabWidget->setCurrentIndex(idx);
       emit sigUpdateBtn();
    });

    // 左移tab按键
    connect(ui->moveLeftBtn,&QPushButton::clicked,ui->tabWidget,[&](){
        int idx = ui->tabWidget->currentIndex()-1;
        ui->tabWidget->setCurrentIndex(idx);
        emit sigUpdateBtn();
    });

    // 右移tab按键
    connect(ui->moveRightBtn,&QPushButton::clicked,ui->tabWidget,[&](){
        int idx = ui->tabWidget->currentIndex()+1;
        ui->tabWidget->setCurrentIndex(idx);
        qDebug() << ui->tabWidget->tabBar()->currentIndex() << " "<<ui->tabWidget->tabBar()->count();
        qDebug() << ui->tabWidget->currentIndex() << " "<<ui->tabWidget->count();
        emit sigUpdateBtn();
    });

    // 3个删除相关tab
    // 删除当前tab
    connect(ui->delCurTab,&QPushButton::clicked,ui->tabWidget,[&](){
        int idx = ui->tabWidget->currentIndex();
        qDebug() << idx;
        // 删除tab
        auto widget = ui->tabWidget->widget(idx);
        ui->tabWidget->removeTab(idx);
        if(widget)delete widget;
        // 更新tab名称
        for(int i=idx;i<ui->tabWidget->count();++i){
            ui->tabWidget->setTabText(i,QString("tab_%1").arg(i));
        }
        emit sigUpdateBtn();
    });

    // 删除左边tab
    connect(ui->delLeftBtn,&QPushButton::clicked,ui->tabWidget,[&](){
       int idx = ui->tabWidget->currentIndex()-1;
       qDebug() << idx;
       // 删除tab
       auto widget = ui->tabWidget->widget(idx);
       ui->tabWidget->removeTab(idx);
       if(widget!=nullptr)delete widget;
       // 更新tab名称
       for(int i=idx;i<ui->tabWidget->count();++i){
           ui->tabWidget->setTabText(i,QString("tab_%1").arg(i));
       }
       emit sigUpdateBtn();
    });

    // 删除右边tab
    connect(ui->delRightBtn,&QPushButton::clicked,ui->tabWidget,[&](){
       int idx = ui->tabWidget->currentIndex()+1;
       qDebug() << idx;
       // 删除tab
       auto widget = ui->tabWidget->widget(idx);
       ui->tabWidget->removeTab(idx);
       if(widget!=nullptr)delete widget;
       // 更新tab名称
       for(int i=idx;i<ui->tabWidget->count();++i){
           ui->tabWidget->setTabText(i,QString("tab_%1").arg(i));
       }
       emit sigUpdateBtn();
    });

    // 添加一个tableView
    connect(ui->addTableView,&QPushButton::clicked,ui->tabWidget,[&](){
        auto newTableView = new QTableView(ui->tabWidget);
        int idx = ui->tabWidget->currentIndex();
        qDebug() << idx;
        // 删除tab
        auto widget = ui->tabWidget->widget(idx);
        ui->tabWidget->removeTab(idx);
        if(widget)delete widget;
        // 插入新tab
        ui->tabWidget->insertTab(idx,newTableView,QString("tab_%1").arg(idx));
        ui->tabWidget->setCurrentIndex(idx);
    });

    // 添加一个treeView
    connect(ui->addTreeView,&QPushButton::clicked,ui->tabWidget,[&](){
        auto newTreeView = new QTreeView(ui->tabWidget);
        int idx = ui->tabWidget->currentIndex();
        qDebug() << idx;
        // 删除tab
        auto widget = ui->tabWidget->widget(idx);
        ui->tabWidget->removeTab(idx);
        if(widget)delete widget;
        // 插入新tab
        ui->tabWidget->insertTab(idx,newTreeView,QString("tab_%1").arg(idx));
        ui->tabWidget->setCurrentIndex(idx);
    });

    // 设置当前tab为MyForm
    connect(ui->setTabMyForm,&QPushButton::clicked,ui->tabWidget,[&](){
        auto myForm = new MyForm(ui->tabWidget);
        int idx = ui->tabWidget->currentIndex();
        qDebug() << idx;
        auto widget = ui->tabWidget->widget(idx);
        ui->tabWidget->removeTab(idx);
        if(widget)delete widget;
        ui->tabWidget->insertTab(idx,myForm,QString("tab_%1").arg(idx));
        ui->tabWidget->setCurrentIndex(idx);
    });

    // 设置当前页面为动物展示页面
    connect(ui->setTabAnimalFrom,&QPushButton::clicked,ui->tabWidget,[&](){
        auto animalShowWidget = new AnimalShowWidget(ui->tabWidget);
        int idx = ui->tabWidget->currentIndex();
        qDebug() << idx;
        QWidget* widget = ui->tabWidget->widget(idx);
        ui->tabWidget->removeTab(idx);
        if(widget){ // removeTab 并不会移除掉tab对应的widget
            delete widget;
//            qDebug() << "delete widget";
        }
        ui->tabWidget->insertTab(idx,animalShowWidget,QString("tab_%1").arg(idx));
        ui->tabWidget->setCurrentIndex(idx);
    });

    // 添加猫
    connect(ui->addCat,&QPushButton::clicked,this,[&](){
        Animal* cat = new Cat("cat",12,"miao~");
        AnimalManager::Instance().addAnimal(QSharedPointer<Animal>(cat));

    });

    // 添加狗
    connect(ui->addDog,&QPushButton::clicked,this,[&](){
        Animal* dog = new Dog("dog",10,"wang~");
        AnimalManager::Instance().addAnimal(QSharedPointer<Animal>(dog));
    });
}

Widget::~Widget()
{
    delete ui;
}

void Widget::slotUpdateBtn()
{
    int idx = ui->tabWidget->currentIndex();
    int count = ui->tabWidget->count();
    if(idx <= 0){
        ui->delLeftBtn->setEnabled(false);
        ui->moveLeftBtn->setEnabled(false);
    }else{
        ui->delLeftBtn->setEnabled(true);
        ui->moveLeftBtn->setEnabled(true);
    }
    if(idx >= count-1){
        ui->delRightBtn->setEnabled(false);
        ui->moveRightBtn->setEnabled(false);
    }else{
        ui->delRightBtn->setEnabled(true);
        ui->moveRightBtn->setEnabled(true);
    }
}
